package com.px.business.service.impl;

import com.px.business.domain.*;
import com.px.business.domain.dto.EvaluationExpertDTO;
import com.px.business.domain.dto.ExpertScoreDTO;
import com.px.business.mapper.EvaluationExpertMapper;
import com.px.business.mapper.EvaluationItemMapper;
import com.px.business.mapper.EvaluationScoreMapper;
import com.px.business.service.IEvaluationApplicationService;
import com.px.business.service.IEvaluationExpertService;
import com.px.common.core.domain.entity.SysUser;
import com.px.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 分配专家Service业务层处理
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Service
public class EvaluationExpertServiceImpl implements IEvaluationExpertService {
    @Autowired
    private EvaluationExpertMapper evaluationExpertMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private EvaluationItemMapper evaluationItemMapper;

    @Autowired
    private EvaluationScoreMapper evaluationScoreMapper;

    @Autowired
    private IEvaluationApplicationService evaluationApplicationService;


    /**
     * 查询分配专家
     *
     * @param evaluationExpertId 分配专家主键
     * @return 分配专家
     */
    @Override
    public EvaluationExpert selectEvaluationExpertByEvaluationExpertId(Long evaluationExpertId) {
        return evaluationExpertMapper.selectEvaluationExpertByEvaluationExpertId(evaluationExpertId);
    }

    /**
     * 查询分配专家列表
     *
     * @param evaluationExpert 分配专家
     * @return 分配专家
     */
    @Override
    public List<EvaluationExpert> selectEvaluationExpertList(EvaluationExpert evaluationExpert) {
        return evaluationExpertMapper.selectEvaluationExpertList(evaluationExpert);
    }

    /**
     * 新增分配专家
     *
     * @param evaluationExpert 分配专家
     * @return 结果
     */
    @Override
    public int insertEvaluationExpert(EvaluationExpert evaluationExpert) {
        return evaluationExpertMapper.insertEvaluationExpert(evaluationExpert);
    }

    /**
     * 修改分配专家
     *
     * @param evaluationExpert 分配专家
     * @return 结果
     */
    @Override
    public int updateEvaluationExpert(EvaluationExpert evaluationExpert) {
        return evaluationExpertMapper.updateEvaluationExpert(evaluationExpert);
    }

    /**
     * 完成专家评分
     *
     * @param evaluationExpert 分配专家
     * @return 结果
     */
    @Override
    public int finishEvaluation(EvaluationExpert evaluationExpert) {
        int row = evaluationExpertMapper.finishEvaluationExpert(evaluationExpert);
        List<EvaluationExpert> evaluationExpertList = evaluationExpertMapper.selectEvaluationExpertByFinishedAtNotNull(evaluationExpert);
        if (evaluationExpertList.size() == 0) {
            evaluationApplicationService.changeState(new ApplicationStatus(evaluationExpert.getEvaluationApplicationId(), 50));
        }
        return row;
    }

    /**
     * 批量删除分配专家
     *
     * @param evaluationExpertIds 需要删除的分配专家主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationExpertByEvaluationExpertIds(Long[] evaluationExpertIds) {
        return evaluationExpertMapper.deleteEvaluationExpertByEvaluationExpertIds(evaluationExpertIds);
    }

    /**
     * 删除分配专家信息
     *
     * @param evaluationExpertId 分配专家主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationExpertByEvaluationExpertId(Long evaluationExpertId) {
        return evaluationExpertMapper.deleteEvaluationExpertByEvaluationExpertId(evaluationExpertId);
    }

    /**
     * 查询专家人员
     *
     * @return 结果
     */
    @Override
    public List<SysUser> selectUserByRoleId() {
        return sysUserMapper.selectUserByRoleId();
    }

    /**
     * 申请安排的专家
     *
     * @param evaluationApplicationId 申请标识
     * @return 结果
     */
    @Override
    public Map selectExpertByEvaluationApplicationId(Long evaluationApplicationId) {
        Map map=new HashMap();
        List<Long> list = new ArrayList<>();
        List<Long> listout = new ArrayList<>();
        EvaluationExpert evaluationExpert = new EvaluationExpert();
        evaluationExpert.setEvaluationApplicationId(evaluationApplicationId);
        List<EvaluationExpert> evaluationExperts = evaluationExpertMapper.selectEvaluationExpertList(evaluationExpert);
        for (EvaluationExpert evaluationExpert1 : evaluationExperts) {
            if(evaluationExpert1.getDeptId()==105){
                listout.add(evaluationExpert1.getExpertBy());
            }else{
                list.add(evaluationExpert1.getExpertBy());
            }

        }
        map.put("Experts",list);
        map.put("ExpertOuts",listout);
        return map;

    }

    /**
     * 批量插入
     *
     * @param dto 得分
     * @return 结果
     */
    @Override
    public boolean expertScoreSava(ExpertScoreDTO dto) {
        //获取项目
        List<EvaluationItem> evaluationItems = evaluationItemMapper.selectEvaluationItemList(new EvaluationItem(dto.getEvaluationApplicationId()));
        //获取已存在数据
        List<EvaluationExpert> evaluationExperts = evaluationExpertMapper.selectEvaluationExpertList(new EvaluationExpert(dto.getEvaluationApplicationId()));
        //重组集合
        List<Long> inputExpertBysIn = Arrays.asList(dto.getExpertBysIn());
        List<Long> inputExpertBysOut = Arrays.asList(dto.getExpertBysOut());
        List<Long> inputExpertBys = new ArrayList<>();
        inputExpertBys.addAll(inputExpertBysIn);
        inputExpertBys.addAll(inputExpertBysOut);
        //现有集合
        List<Long> existExpertsBys = evaluationExperts.stream().map(EvaluationExpert::getExpertBy).collect(Collectors.toList());
        //带插入集合
        List<Long> insertExpertByList = inputExpertBys.stream().filter(item -> !existExpertsBys.contains(item)).collect(Collectors.toList());
        //取消集合
        List<Long> deleteExpertByList = existExpertsBys.stream().filter(item -> !inputExpertBys.contains(item)).collect(Collectors.toList());
        for (Long l : insertExpertByList) {
            List<EvaluationScore> evaluationScoreList = new ArrayList<>();
            //院内还是院外
            int expertType = 0;
            if (inputExpertBysIn.contains(l)) {
                expertType = 1;
            } else if (inputExpertBysOut.contains(l)) {
                expertType = 2;
            }
            EvaluationExpert evaluationExpert = new EvaluationExpert(dto.getEvaluationApplicationId(), expertType, l);
            //插入主表数据
            int expert = evaluationExpertMapper.insertEvaluationExpert(evaluationExpert);
            //插入明细
            for (EvaluationItem evaluationItem : evaluationItems) {
                evaluationScoreList.add(new EvaluationScore(evaluationExpert.getEvaluationExpertId(), evaluationItem.getEvaluationItemId()));
            }
            evaluationScoreMapper.saveEvaluationScoreList(evaluationScoreList);

        }
        if (deleteExpertByList.size() > 0) {
            evaluationExpertMapper.deleteEvaluationExpertByIndicatorIds(dto.getEvaluationApplicationId(), deleteExpertByList);
        }
        evaluationApplicationService.changeState(new ApplicationStatus(dto.getEvaluationApplicationId(), 40));

        return true;
    }

    /**
     * 查询评分服务
     *
     * @param evaluationExpert 专家
     * @return 结果
     */
    @Override
    public List<EvaluationExpertDTO> getEvaluationExpertDTO(EvaluationExpert evaluationExpert) {
        List<EvaluationExpertDTO> evaluationExpertDTOS = new ArrayList<>();
        //条件查询华智能和
        List<EvaluationExpert> evaluationExperts = evaluationExpertMapper.selectEvaluationExpertList(evaluationExpert);
        List<Long> ExpertIds = evaluationExperts.stream().map(EvaluationExpert::getEvaluationExpertId).collect(Collectors.toList());
        //全部申请
        List<EvaluationScore> evaluationScores = evaluationScoreMapper.getEvaluationScoreIds(ExpertIds);

        for (EvaluationExpert evaluationExpert1 : evaluationExperts) {
            EvaluationExpertDTO evaluationExpertDTO = new EvaluationExpertDTO();
            evaluationExpertDTO.setEvaluationExpertId(evaluationExpert1.getEvaluationExpertId());
            evaluationExpertDTO.setEvaluationApplicationId(evaluationExpert1.getEvaluationApplicationId());
            evaluationExpertDTO.setExpertBy(evaluationExpert1.getExpertBy());
            List<EvaluationScore> evaluationScoreList = evaluationScores.stream().filter(item -> item.getEvaluationExpertId().equals(evaluationExpert1.getEvaluationExpertId())).collect(Collectors.toList());


            evaluationExpertDTO.setEvaluationScoreList(evaluationScoreList);
            evaluationExpertDTOS.add(evaluationExpertDTO);
        }

        return evaluationExpertDTOS;

    }


    /**
     * @param list 指标
     * @param t    指标
     */
    private void recursionFn(List<EvaluationIndicator> list, EvaluationIndicator t) {
        // 得到子节点列表
        List<EvaluationIndicator> childList = getChildList(list, t);
        t.setChildren(childList);
        for (EvaluationIndicator tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    private boolean hasChild(List<EvaluationIndicator> list, EvaluationIndicator t) {
        return getChildList(list, t).size() > 0;
    }

    private List<EvaluationIndicator> getChildList(List<EvaluationIndicator> list, EvaluationIndicator t) {
        List<EvaluationIndicator> tList = new ArrayList<>();
        Iterator<EvaluationIndicator> it = list.iterator();
        while (it.hasNext()) {
            EvaluationIndicator n = it.next();
            if (n.getParentId().longValue() == t.getEvaluationIndicatorId().longValue()) {
                tList.add(n);
            }
        }
        return tList;
    }

}
